﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.admin.product
{
    public partial class product_integratepurchas_edit : System.Web.UI.Page
    {
        protected string strqq;
        protected string strmm;
        protected int strid;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              
              
                if (ChangeHope.WebPage.PageRequest.GetString("id") != "")
                {
                    BandInfo(Convert.ToInt32(ChangeHope.WebPage.PageRequest.GetString("id")));
                }
                InitWebControl();
                if (ViewState["ID"] == null)
                {
                    YXShop.Common.PromptInfo.Popedom("002001002", "对不起，您没有权限进行新增");
                }
                else
                {
                    YXShop.Common.PromptInfo.Popedom("002001004", "对不起，您没有权限进行编辑");
                }
            }
        }
        /// <summary>
        /// 验证
        /// </summary>
        private void InitWebControl()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            this.txtProductName.Attributes.Add("readonly", "readonly");
            this.txtProductName.Attributes.Add("onclick", "selectFile('integratepurchasProduct',new Array(" + this.hfid.ClientID + "," + this.txtProductName.ClientID + "),310,550,'" + sp.DummyPaht + "');");
            ChangeHope.WebPage.WebControl.SetDate(this.txtEndTime);
            ChangeHope.WebPage.WebControl.SetDate(this.txtStartTime);
            ChangeHope.WebPage.WebControl.Validate(this.txtProductName, "选择团购商品", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtDeposit, "输入保证金,将作为会员参数团购缴纳数字", "isfloat", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtStartTime, "输入团购开始日期", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtEndTime, "输入截止日期", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtCount, "输入团购数量", "isint", "必填", "该项为必填项，必须为数字。");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
            this.txtProductName.Attributes.Add("readonly", "readonly");
        }
        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Save();
        }

        protected void BandInfo(int id)
        {
            YXShop.BLL.Product.Product_IntegratePurchas bll = new YXShop.BLL.Product.Product_IntegratePurchas();
            YXShop.Model.Product.Product_IntegratePurchas model = bll.GetModelID(id);
            if (model != null)
            {
                this.txtProductName.Text = model.ProductName;
                this.hfid.Value = model.ProductID.ToString();
                this.txtStartTime.Text = model.StartTime.ToString();
                this.txtEndTime.Text = model.EndTime.ToString();
                this.txtDeposit.Text = model.Deposit.ToString();
                this.txtCount.Text = model.Count.ToString();
                this.txtDonateIntegral.Text = model.DonateIntegral.ToString();
                this.txtDescription.Text = model.Description;
                strmm = model.LadderCount;
                strqq = model.LadderPrice;
                string uu = model.LadderCount;
                String[] ar = uu.Split(',');
                strid = ar.Length;
                txtsl.Text = ar[0].ToString();
                string kk = model.LadderPrice;
                String[] ab = kk.Split(',');
                txtjg.Text = ab[0].ToString();
                string mystr1 = "<script language='javascript'>StrAdd1();</script>";
                if (!this.IsClientScriptBlockRegistered("buttonclick"))
                {
                    this.RegisterStartupScript("buttonclick", mystr1);
                }
                ViewState["ID"] = id;
            }
        }
        protected string ProductName(string ProductId)
        {
            string reStr = string.Empty;
            string str = "";
            if (!string.IsNullOrEmpty(ProductId))
            {
                YXShop.BLL.Product.ProductInfo dll = new YXShop.BLL.Product.ProductInfo();
                YXShop.Model.Product.ProductInfo model = dll.GetModel(Convert.ToInt32(ProductId));
                if (model != null)
                {
                    str = model.pro_Name;
                }
            }
            return str;
        }
        /// <summary>
        /// 保存信息
        /// </summary>
        protected void Save()
        {
            int Conut = Convert.ToInt32(ChangeHope.WebPage.PageRequest.GetFormString("Conut"));
            string CountStr = "";
            string Countjg = "";
            if (txtsl.Text != "")
            {
                if (!ChangeHope.Common.ValidateHelper.IsNumber(txtsl.Text.Trim()))
                {
                    this.ltlMsg.Text = "操作失败，数量请输入数字。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;

                }
                if (int.Parse(txtsl.Text.Trim()) < 0)
                {
                    this.ltlMsg.Text = "操作失败，数量不能少于1。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
            if (txtjg.Text != "")
            {
                if (!ChangeHope.Common.ValidateHelper.IsDecimal(txtjg.Text.Trim()))
                {
                    this.ltlMsg.Text = "操作失败，请输入正确价格。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                if (float.Parse(txtjg.Text.Trim()) < 0)
                {
                    this.ltlMsg.Text = "操作失败，价格不能为负数！。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                
            }
            if (Conut != 1)
            {
                for (int i = 0; i < Conut; i++)
                {
                    CountStr = txtsl.Text.Trim() + "," + Request.Params["txtwsl"].ToString();
                    Countjg = txtjg.Text.Trim() + "," + Request.Params["txtwjg"].ToString();
                }
            }
            else
            {
                CountStr = txtsl.Text.Trim();
                Countjg = txtjg.Text.Trim();
            }
            YXShop.BLL.Product.Product_IntegratePurchas bll = new YXShop.BLL.Product.Product_IntegratePurchas();
            YXShop.Model.Product.Product_IntegratePurchas model = new YXShop.Model.Product.Product_IntegratePurchas();
            model.ProductID =Convert.ToInt32(this.hfid.Value);
            model.ProductName = this.txtProductName.Text;
            if(this.txtStartTime.Text!="")
            {
                if(Convert.ToDateTime(txtStartTime.Text)<Convert.ToDateTime(System.DateTime.Now))
                {
                    this.ltlMsg.Text = "操作失败，开始时间小于现在时间！。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
            if(this.txtEndTime.Text!="")
            {
                if(Convert.ToDateTime(txtEndTime.Text)<=Convert.ToDateTime(txtStartTime.Text))
                {
                    this.ltlMsg.Text = "操作失败，结束时间小于或等于开始时间！。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
            model.StartTime = Convert.ToDateTime(this.txtStartTime.Text);
            model.EndTime = Convert.ToDateTime(this.txtEndTime.Text);
            model.Deposit = Convert.ToDecimal(this.txtDeposit.Text);
            model.Count = Convert.ToInt32(this.txtCount.Text);
           
            if (txtDonateIntegral.Text != "")
            {
                if (!ChangeHope.Common.ValidateHelper.IsNumber(txtDonateIntegral.Text.Trim()))
                {
                    this.ltlMsg.Text = "操作失败，请输入正确积分！。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                if (int.Parse(txtDonateIntegral.Text) < 0)
                {
                    this.ltlMsg.Text = "操作失败，积分不能小于0！。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                model.DonateIntegral = Convert.ToDecimal(this.txtDonateIntegral.Text);
            }
            else
            {
                model.DonateIntegral = Convert.ToDecimal(0);
            }       
            model.LadderCount = CountStr;
            model.LadderPrice = Countjg;
            model.Description = this.txtDescription.Text;
            if (ViewState["ID"] == null)
            {
                if (bll.Add(model) != 0)
                {
                    this.txtProductName.Text = string.Empty;
                    this.ltlMsg.Text = "操作成功，添加信息保存成功。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionOk";
                }
            }
            else
            {
                model.ID = Convert.ToInt32(ViewState["ID"].ToString());
                if (bll.Update(model) != 0)
                {
                    this.txtProductName.Text = string.Empty;
                    this.ltlMsg.Text = "操作成功，修改信息保存成功。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionOk";
                }
            }
        }
    }
}
